//
//  Globalize.m
//  ICE_Player
//
//  Created by  on 11-10-19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "Globalize.h"

static Globalize* g_globalize_obj = nil;

static NSString* g_str_table[EG_STR_ID_COUNT] = {0};

static void init_g_str_table()
{
    for (int i = 0; i < EG_STR_ID_COUNT; ++i)
    {
        if (g_str_table[i])
        {
            [g_str_table[i] release];
            g_str_table[i] = nil;
        }
    }
    //Config
    g_str_table[EG_STR_ID_CFG_CONFIG] = [NSLocalizedString(@"CONFIGURE", nil) copy];
    g_str_table[EG_STR_ID_CFG_ROTATION] = [NSLocalizedString(@"ROTATION LOCK", nil) copy];
    g_str_table[EG_STR_ID_CFG_SUBTITLE] = [NSLocalizedString(@"SUBTITLE", nil) copy];
    g_str_table[EG_STR_ID_CFG_TEXT_INFO] = [NSLocalizedString(@"TEXT INFO", nil) copy];
    g_str_table[EG_STR_ID_CFG_LOOP_MODE] = [NSLocalizedString(@"LOOP MODE", nil) copy];
    g_str_table[EG_STR_ID_CFG_PRESS_ME] = [NSLocalizedString(@"press me", nil) copy];
    g_str_table[EG_STR_ID_CFG_WEB] = [NSLocalizedString(@"Produced by:www.sweety-apps.com", nil) copy];
    g_str_table[EG_STR_ID_CFG_EMAIL] = [NSLocalizedString(@"Email:sweety.apps.cn@gmail.com", nil) copy];
    //List and Cell
    g_str_table[EG_STR_ID_LIST_CELL_PLAY] = [NSLocalizedString(@"Play", nil) copy];
    //MessageBox
    g_str_table[EG_STR_ID_MB_OK] = [NSLocalizedString(@"OK", nil) copy];
    g_str_table[EG_STR_ID_MB_CANCEL] = [NSLocalizedString(@"CANCEL", nil) copy];
    g_str_table[EG_STR_ID_MB_YES] = [NSLocalizedString(@"YES", nil) copy];
    g_str_table[EG_STR_ID_MB_NO] = [NSLocalizedString(@"NO", nil) copy];
    g_str_table[EG_STR_ID_MB_WIFI_TITLE] = [NSLocalizedString(@"Input URL to your web explorer:", nil) copy];
    g_str_table[EG_STR_ID_MB_DELETE_TITLE] = [NSLocalizedString(@"Delete this file?", nil) copy];
    g_str_table[EG_STR_ID_MB_FILE_ERROR_TITLE] = [NSLocalizedString(@"Can not open file", nil) copy];
    g_str_table[EG_STR_ID_MB_REACH_EDGE] = [NSLocalizedString(@"No file then.", nil) copy];
    //BG View
    g_str_table[EG_STR_ID_BG_CONFIG_BTN] = [NSLocalizedString(@"Configure", nil) copy];
    g_str_table[EG_STR_ID_BG_WIFI_BTN] = [NSLocalizedString(@"WIFI File Sharing", nil) copy];
    g_str_table[EG_STR_ID_BG_LOCK_BTN] = [NSLocalizedString(@"Lock Files", nil) copy];
    //Pass Words
    g_str_table[EG_STR_ID_PSW_SET] = [NSLocalizedString(@"Set Passwords", nil) copy];
    g_str_table[EG_STR_ID_PSW_INPUT] = [NSLocalizedString(@"Passwords(6 numbers)", nil) copy];
    g_str_table[EG_STR_ID_PSW_WRONG] = [NSLocalizedString(@"Wrong Passwords!", nil) copy];
    g_str_table[EG_STR_ID_PSW_CONFIRM] = [NSLocalizedString(@"Handling...", nil) copy];
    g_str_table[EG_STR_ID_PSW_RESET_PSW] = [NSLocalizedString(@"ResetPassword", nil) copy];
    g_str_table[EG_STR_ID_PSW_NEW_PSW_IS] = [NSLocalizedString(@"NewPasswordIs", nil) copy];
    //Player View
    g_str_table[EG_STR_ID_PLAYER_RET_BTN] = [NSLocalizedString(@"Go Back", nil) copy];
    g_str_table[EG_STR_ID_PLAYER_SCREEN_MODE] = [NSLocalizedString(@"Screen Adjust", nil) copy];
    g_str_table[EG_STR_ID_PLAYER_PLAY] = [NSLocalizedString(@"Playing", nil) copy];
    g_str_table[EG_STR_ID_PLAYER_PAUSE] = [NSLocalizedString(@"Paused", nil) copy];
    g_str_table[EG_STR_ID_PLAYER_FINGER_DRAG] = [NSLocalizedString(@"<-  finger drag for file changing ->", nil) copy];
    //Low Wnd
    g_str_table[EG_STR_ID_LOW_WND_SIZE] = [NSLocalizedString(@"Size", nil) copy];
    g_str_table[EG_STR_ID_LOW_WND_DURATION] = [NSLocalizedString(@"Duration", nil) copy];
    //Loop Mode
    g_str_table[EG_STR_ID_LOOP_MODE_NO_LOOP] = [NSLocalizedString(@"No Loop", nil) copy];
    g_str_table[EG_STR_ID_LOOP_MODE_SINGLE_LOOP] = [NSLocalizedString(@"Single Loop", nil) copy];
    g_str_table[EG_STR_ID_LOOP_MODE_LIST_LOOP] = [NSLocalizedString(@"List Loop", nil) copy];
    g_str_table[EG_STR_ID_LOOP_MODE_RANDOM] = [NSLocalizedString(@"Random", nil) copy];
    //Empty String
    g_str_table[EG_STR_ID_EMPTY] = @"";
}

NSString* globalize_string_by_id(E_GLOBALIZE_STRING_ID str_id)
{
    if (!g_str_table[EG_STR_ID_CFG_WEB])
    {
        init_g_str_table();
    }
    return g_str_table[str_id];
}



@implementation Globalize

@synthesize hidden_labels;


+ (id)current_global
{
    if (g_globalize_obj == nil)
    {
        g_globalize_obj = [[Globalize alloc]init];
    }
    return g_globalize_obj;
}

- (id)init
{
    id ret_val = [super init];
    p_label_arrays = malloc(sizeof(ICE_Label_View*)*EG_LBL_ID_COUNT);
    memset(p_label_arrays, 0, sizeof(ICE_Label_View*)*EG_LBL_ID_COUNT);
    p_string_arrays = malloc(sizeof(ICE_String*)*EG_STR_ID_COUNT);
    memset(p_string_arrays, 0, sizeof(ICE_String*)*EG_STR_ID_COUNT);
    p_image_arrays = malloc(sizeof(ICE_Image_View*)*EG_IMG_ID_COUNT);
    memset(p_image_arrays, 0, sizeof(ICE_Image_View*)*EG_IMG_ID_COUNT);
    return ret_val;
}

- (void)dealloc
{
    free(p_image_arrays);
    free(p_string_arrays);
    free(p_label_arrays);
    [super dealloc];
}

- (ICE_Label_View*)localize_label:(ICE_Label_View*)label with_language:(E_GLOBALIZE_LANGUAGE)language of_id:(E_GLOBALIZE_LABLE_ID)obj_id
{
    if (!p_label_arrays[obj_id])
    {
        p_label_arrays[obj_id] = label;
    }
    return label;
}

- (ICE_String*)localize_string:(ICE_String*)string with_language:(E_GLOBALIZE_LANGUAGE)language of_id:(E_GLOBALIZE_STRING_ID)obj_id
{
    if (!p_string_arrays[obj_id])
    {
        p_string_arrays[obj_id] = string;
    }
    return string;
}

- (ICE_Image_View*)localize_image:(ICE_Image_View*)image_view with_language:(E_GLOBALIZE_LANGUAGE)language of_id:(E_GLOBALIZE_STRING_ID)obj_id
{
    if (!p_image_arrays[obj_id])
    {
        p_image_arrays[obj_id] = image_view;
    }
    return image_view;
}

- (ICE_Label_View*)localize_label_by_current_language:(ICE_Label_View*)label of_id:(E_GLOBALIZE_LABLE_ID)obj_id
{
    if (!p_label_arrays[obj_id])
    {
        p_label_arrays[obj_id] = label;
    }
    return label;
}

- (ICE_String*)localize_string_by_current_language:(ICE_String*)string of_id:(E_GLOBALIZE_STRING_ID)obj_id
{
    if (!p_string_arrays[obj_id])
    {
        p_string_arrays[obj_id] = string;
    }
    return string;
}

- (ICE_Image_View*)localize_image_by_current_language:(ICE_Image_View*)image_view of_id:(E_GLOBALIZE_STRING_ID)obj_id
{
    if (!p_image_arrays[obj_id])
    {
        p_image_arrays[obj_id] = image_view;
    }
    return image_view;
}

- (void)localize_all_label
{
    int i = 0;
    for (i = 0; i < EG_LBL_ID_COUNT; ++i)
    {
    }
}

- (void)localize_all_string
{
    int i = 0;
    for (i = 0; i < EG_STR_ID_COUNT; ++i)
    {
    }
}

- (void)localize_all_image
{
    int i = 0;
    for (i = 0; i < EG_IMG_ID_COUNT; ++i)
    {
    }
}

- (void)setHidden_labels:(BOOL)enabled
{
    if (hidden_labels == enabled)
    {
        return;
    }
    int i = 0;
    for (i = 0; i < EG_LBL_ID_COUNT; ++i)
    {
        if (p_label_arrays[i])
        {
            p_label_arrays[i].hidden = enabled;
        }
    }
    hidden_labels = enabled;
}

@end
